package com.swpu.service_step.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_step.entity.StepRecord;
import com.swpu.service_step.entity.StepTarget;
import com.swpu.service_step.entity.WxAuthorizeVo;
import com.swpu.service_step.service.StepRecordService;
import com.swpu.service_step.service.StepTargetService;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-03-03
 */
@RestController
@RequestMapping("/service_step/step-record")
public class StepRecordController {
    @Autowired
    private StepRecordService stepRecordService;
    @Autowired
    private StepTargetService stepTargetService;
    @PostMapping("/getUserStepData")
    @Encrypt
    @Decrypt
     public R getUserStepData(@RequestBody ResultEncryption resultEncryption, WxAuthorizeVo wxAuthorizeVo, HttpServletRequest request) throws Exception {
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        R todayStepRes = stepRecordService.decodeData(wxAuthorizeVo);
        return todayStepRes;
    }
    @GetMapping("/getStepByDate")
    public R getStepByDate(HttpServletRequest request,@RequestParam("queryDate") String dayStr){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        DateTime queryDate = DateUtil.parseDateTime(dayStr);
        StepRecord stepRecord = stepRecordService.getStepsByDate(openid, queryDate);
        return R.ok().data("stepRecord",stepRecord);
    }

    @GetMapping("getStepTargeteList")
    public R getStepTargetList(){
        List<StepTarget> stepTarget = stepTargetService.getStepTarget();
        return R.ok().data("stepTargetList",stepTarget);
    }


}

